package com.ruoyi.common.core.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * HTTP工具类
 * 
 * @author ruoyi
 */
public class HttpUtils
{
    /**
     * 发送POST请求
     * 
     * @param host 主机地址
     * @param path 请求路径
     * @param method 请求方法
     * @param headers 请求头
     * @param querys 查询参数
     * @param bodys 请求体
     * @return 响应结果
     * @throws Exception 异常
     */
    public static String doPost(String host, String path, String method, 
                               Map<String, String> headers, 
                               Map<String, String> querys, 
                               Map<String, String> bodys) throws Exception
    {
        // 构建URL
        StringBuilder urlBuilder = new StringBuilder(host + path);
        if (querys != null && !querys.isEmpty())
        {
            urlBuilder.append("?");
            for (Map.Entry<String, String> entry : querys.entrySet())
            {
                urlBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
            // 删除最后一个&
            urlBuilder.deleteCharAt(urlBuilder.length() - 1);
        }
        
        URL url = new URL(urlBuilder.toString());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        
        // 设置请求方法和属性
        connection.setRequestMethod(method);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setConnectTimeout(30000);
        connection.setReadTimeout(30000);
        
        // 设置请求头
        if (headers != null && !headers.isEmpty())
        {
            for (Map.Entry<String, String> entry : headers.entrySet())
            {
                connection.setRequestProperty(entry.getKey(), entry.getValue());
            }
        }
        
        // 发送请求体
        if (bodys != null && !bodys.isEmpty())
        {
            StringBuilder bodyBuilder = new StringBuilder();
            for (Map.Entry<String, String> entry : bodys.entrySet())
            {
                bodyBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
            if (bodyBuilder.length() > 0)
            {
                bodyBuilder.deleteCharAt(bodyBuilder.length() - 1);
            }
            
            try (OutputStream os = connection.getOutputStream())
            {
                os.write(bodyBuilder.toString().getBytes(StandardCharsets.UTF_8));
                os.flush();
            }
        }
        
        // 读取响应
        StringBuilder response = new StringBuilder();
        try (InputStream is = connection.getInputStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)))
        {
            String line;
            while ((line = reader.readLine()) != null)
            {
                response.append(line);
            }
        }
        
        return response.toString();
    }
}
